import 'package:flutter/material.dart';
import 'package:flutter_shopcar_test/model/goods_list_model.dart';
import 'package:flutter_shopcar_test/router.dart';
import 'package:get/get.dart';

import 'home_page_logic.dart';

class HomePagePage extends StatelessWidget {
  HomePagePage({super.key});

  final logic = Get.put(HomePageLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('商品列表'),
        centerTitle: true,
        actions: [
          IconButton(
            onPressed: () {
              Get.toNamed(RouterName.shopCartPage)?.then((e) {
                logic.queryGoodsList();
              });
            },
            icon: const Icon(Icons.shopping_cart),
          ),
          IconButton(
            onPressed: logic.goToOrder,
            icon: const Icon(Icons.book),
          )
        ],
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
      ),
      body: _buildContentWidget(),
      // floatingActionButton: Container(
      //   padding: const EdgeInsets.only(bottom: 50, right: 20),
      //   child: FloatingActionButton(
      //     onPressed: () =>  Get.toNamed(RouterName.shopCartPage),
      //     child: const Icon(Icons.shopping_cart),
      //   ),
      // ),
    );
  }

  Widget _buildContentWidget() {
    return GetBuilder<HomePageLogic>(
      builder: (logic){
        return Column(
          children: [
            _buildGoodsList(),
            _buildBottomWidget()
          ],
        );
      },
    );
  }

  Widget _buildGoodsList() {
    if(logic.goodsList.isEmpty) {
      return const Expanded(child: Center(child: Text("暂无数据", style: TextStyle(fontSize: 14),)));
    }
    return Expanded(
      child: ListView.separated(
        itemBuilder: (context, index){
         GoodsListModel model = logic.goodsList[index];
         return _buildItemWidget(model);
        },
        separatorBuilder: (context, index){
          return Container(height: 10);
        },
        itemCount: logic.goodsList.length,
        shrinkWrap: true,
        padding: const EdgeInsets.all(12),
      ),
    );
  }

  Widget _buildItemWidget(GoodsListModel model) {
    return Container(
      padding: const EdgeInsets.all(12),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8)
      ),
      child: Row(
        children: [
          Container(
            color: const Color(0xFFdddddd),
            width: 100,
            height: 100,
            child: const Icon(Icons.image),
          ),
          const SizedBox(width: 12,),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(model.goodsName ?? '', style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),),
                _buildSizedBoxWidget(),
                Text("商品编码：${model.goodsCode ?? ''}", style: const TextStyle(fontSize: 14),),
                _buildSizedBoxWidget(),
                _buildCountWidget(model)
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildCountWidget(GoodsListModel model) {
    return Row(
      children: [
        Text("￥${model.goodsPrice}", style: const TextStyle(fontSize: 14, color: Color(0xFF666666)),),
        Expanded(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              InkWell(
                onTap: () => logic.addCount(model),
                child: _buildContainerWidget(
                  widget: const Text("+", style: TextStyle(fontSize: 16),),
                ),
              ),
              const SizedBox(width: 6,),
              _buildContainerWidget(
                widget: Text("${model.goodsQty}", style: const TextStyle(fontSize: 16),),
              ),
              const SizedBox(width: 6,),
              InkWell(
                onTap: () => logic.subCount(model),
                child: _buildContainerWidget(
                  widget: const Text("-", style: TextStyle(fontSize: 16),),
                ),
              ),
            ],
          ),
        )
      ],
    );
  }

  Widget _buildContainerWidget({required Widget widget}) {
    return Container(
      width: 30,
      height: 30,
      color: const Color(0xFFDDDDDD),
      child: Center(child: widget,),
    );
  }

  Widget _buildBottomWidget() {
    return Container(
      color: Colors.white,
      padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 14),
      child: _buildButtonWidget(),
    );
  }

  Widget _buildButtonWidget() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text("共计：${logic.totalPrice()}", style: const TextStyle(fontSize: 14),),
        InkWell(
          onTap: () => logic.addCart(),
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
            decoration: BoxDecoration(
              color: Colors.blue,
              borderRadius: BorderRadius.circular(4)
            ),
            child: const Text('加入购物车', style: TextStyle(color: Colors.white),),
          ),
        ),
      ],
    );
  }

  Widget _buildSizedBoxWidget() {
    return const SizedBox(height: 10);
  }

}
